<?php
require_once 'IEngine.php';
require_once 'simple_html_dom.php';

class GoogleEngine implements IEngine{
	private $tags = array("Web", "Music", "Movies");
	private $name = "Google";

	function search($term) {
		$results = array();
		$url = "http://www.google.ro/search?hl=en&q=" . urlencode($term);
		$str = file_get_contents($url);
		$page = str_get_html($str);
		$res = $page->find("div#res div ol li");
		$res = $res;
		foreach ($res as $child) {
			$result = array();
			$title = $child->find("a");
			if (count($title) == 0 || !isset($title[0]->attr['class'])) continue;
			$result['title'] = $title[0]->innertext();
			$result['href'] = $title[0]->attr['href'];
			$description = $child->find("div.s");
			if (count($description)) {
			$result['description'] = $description[0]->innertext();
			$result['description'] = strip_tags($result['description'],"<b><em><i><br>");
			} else {
				$result['description'] = "";
			}
			$results[] = $result;
		}
		return $results;
	}

	public function getTags() {
		return $this->tags;
	}
	
	public function getName() {
		return $this->name;
	}

	public function engineLogo() {
		return '<img class="enginelogo" src="http://www.tigregroup.com/images/google_logo.png" />';
	}
}
?>